package edu.gdcc.linearList.LinkedList;

/**
 * 单链表
 * @author Izzy.X
 * @version 2017-01-08 21:20:46
 *
 */

public class SingleLinkedList {
	private Link first;		//ref to first link on list
	
	//constructor
	public SingleLinkedList(){
		this.first = null;
	}
	
	public boolean isEmpty(){
		return first == null;
	}
	
	public void insertFirst(int ii ,double dd) {
		Link temp = new Link(ii, dd);
		temp.next = first;
		first = temp;
	}
	
	public void deleteFirst(){
		System.out.println("delete: "+first);
		first = first.next;	
	}
	
	public void displayList(){
		Link current = first;
		System.out.print("List:");
		while(current != null){
			current.display();
			current = current.next;
			if(current != null){
				System.out.print(" ");
			}
		}
		if(first == null) {
			System.out.print("空链表");
			return;
		}
		System.out.println("");
	}
	
	//----------------test---------------------
	public static void main(String[] agrs){
		SingleLinkedList sll = new SingleLinkedList();
		sll.insertFirst(1, 12.3);
		sll.insertFirst(2, 2.121);
		System.out.println("是否是空链表："+(sll.isEmpty()?"空":"非空"));
		sll.displayList();
		sll.deleteFirst();
		sll.deleteFirst();
		sll.displayList();
	}
}
/**
 * 链结点
 * @author Izzy.X
 * @version 2017-01-08 21:44:09
 *
 */
class  Link {
	private int iData;
	private double dData;
	public Link next;
	
	public Link(int iData,double dData){
		this.iData = iData;
		this.dData = dData;
	}
	
	public void display(){
		System.out.print("{"+iData+","+dData+"}");
	}

	@Override
	public String toString() {
		return "Link [iData=" + iData + ", dData=" + dData + ", next=" + next + "]";
	}
	
}
